/**
 * Created by duwei on 2019/3/4.
 */
var ZiPai_QiePaiWindow = Window.extend({
    _ccsFileName: res.ZiPai_QiePaiWindow_json,
    _paiCount: 80,
    _paiPos: 10.5,
    _blockLayerOpacity: 0,
    /**
     * 初始化
     */
    initWindow: function (data, room) {
        var self = this;
        self.sec = self._nodeList['root.sec'];
        self.huadong = self._nodeList['root.huadong'];
        self.paisNode = self._nodeList['root.paisNode'];
        self.loadingBar = self._nodeList['root.LoadingBar'];
        self.numbg = self._nodeList['root.huadong.numbg'];
        self.index = self._nodeList['root.huadong.numbg.index'];
        self.btn_buqie = self._nodeList['root.btn_buqie'];
        self.shou = self._nodeList['root.shou'];

        self.qieUid = data['UserID'];
        self.leftTime = data['Timeout'] || 0;
        self.room = room;

        if (data['Status'] < 0) {
            self.choose_idx = 0;
            var changeTimeFunc = function () {
                self._nodeList['root.sec'].setString(self.leftTime);
                self.leftTime--;
                if (self.leftTime < 0) {
                    self.unschedule(changeTimeFunc);
                    network.sendPhz(5000, "Qiepai/" + self.choose_idx);
                }
            };
            this.schedule(changeTimeFunc, 1);
            self.castCards();
            self.shou.setVisible(self.qieUid == gameData.uid);
            var pos = self.shou.getPosition();
            self.shou.setPositionX(440);
            self.btn_buqie.setVisible(self.qieUid == gameData.uid);
            self.loadingBar.setVisible(self.qieUid == gameData.uid);
            self.numbg.setVisible(self.qieUid == gameData.uid);
            self._nodeList['root.qiepai_tips1'].setVisible(self.qieUid == gameData.uid);
            self._nodeList['root.qiepai_tips2'].setVisible(self.qieUid != gameData.uid);
        }
    },
    /**
     * 刷新数据，外部调用setData
     */
    refreshData: function (data, room) {
        var self = this;
        self.qieUid = data['UserID'];
        self.leftTime = data['Timeout'] || 0;
        self.room = room;
        if (data['Status'] < 0) {

        } else {
            self.setUpQiePai(data);
        }
    },
    setUpQiePai: function (data) {
        var self = this;
        network.stop();
        var idx = data['Raw'].split('/')[1];
        var endPos = self.paisNode.convertToNodeSpace(data.endPos);
        self.huadong.setVisible(false);
        self.sec.setVisible(false);
        self.loadingBar.setVisible(false);
        self._nodeList['root.qiepai_tips2'].setVisible(false);
        self._nodeList['root.qiepai_tips1'].setVisible(false);
        self._nodeList['root.timeBg'].setVisible(false);
        var posSta = (0 - self._paiCount / 2) * self._paiPos;
        var posEnd = (self._paiCount - self._paiCount / 2) * self._paiPos;
        for (var j = idx; j < self._paiCount; j++) {
            var pai = self.paisNode.getChildByName('pai' + j);
            pai.runAction(cc.sequence(
                cc.moveTo(0.35, posEnd - (i - self._paiCount / 2) * 0.4, 0),
                cc.callFunc(
                    function () {
                        for (var j = idx; j < self._paiCount; j++) {
                            var pai = self.paisNode.getChildByName('pai' + j);
                            pai.setLocalZOrder(-100 + j);
                        }
                    }
                ),
                cc.delayTime(0.35),
                cc.moveTo(0.1, posEnd, 0),
                cc.spawn(cc.moveTo(0.3, endPos.x, endPos.y), cc.rotateTo(0.3, 90), cc.fadeOut(0.3)),
                cc.callFunc(function (node) {
                    cc.log(node.getName());
                    if(node.getName() == ('pai' + (self._paiCount-1))){
                        self.room.setQieCard(self._paiCount-idx);
                    }
                })));
        }
        for (var j = 0; j < idx; j++) {
            var pai = self.paisNode.getChildByName('pai' + j);
            pai.runAction(cc.sequence(
                cc.delayTime(0.55),
                cc.moveTo(0.35, posSta + (i - self._paiCount / 2) * 0.4, 0),
                cc.moveTo(0.1, posSta, 0),
                cc.spawn(cc.moveTo(0.3, endPos.x, endPos.y), cc.rotateTo(0.3, 90), cc.fadeOut(0.3)),
                cc.callFunc(function (node) {
                    cc.log(node.getName());
                    if(node.getName() == ('pai' + (idx-1))){
                        self.room.setQieCard(self._paiCount);
                    }
                })));
        }
    },
    castCards: function () {
        var self = this;
        var sceneid = settingData.local_table[gameData.mapId] || 0;
        var width = self.loadingBar.getContentSize().width;
        var w = mCard.getCardSize().w;
        self._paiPos = (width - w) / self._paiCount;
        self.paisNode.removeAllChildren();
        for (var j = 0; j < self._paiCount; j++) {
            var pai = new cc.Sprite(zipai.res['BigPaiBG_' + sceneid]);
            self.paisNode.addChild(pai);
            pai.x = (j - self._paiCount / 2);
            pai.setName('pai' + j);
            pai.runAction(
                cc.moveTo(0.2, (j - self._paiCount / 2) * self._paiPos, 0).easing(cc.easeOut(1.5))
            )
        }
        if (_.isNumber(self.choose_idx)) {
            self.scheduleOnce(function () {
                self.upPai(self.choose_idx);
                self.initViewEvent();
            }, 0.21)
        }
    },
    initViewEvent: function () {
        var self = this;
        if (self.qieUid != gameData.uid) {
            return;
        }
        TouchUtil.setOnclickListener(self.btn_buqie, function () {
            network.sendPhz(5000, "Qiepai/" + self.choose_idx);
        });
        cc.eventManager.addListener(cc.EventListener.create({
            event: cc.EventListener.TOUCH_ONE_BY_ONE,
            swallowTouches: true,
            onTouchBegan: function (touch, event) {
                if (TouchUtil.isTouchMe(self.huadong, touch, event)) {
                    var locationInNode = self.huadong.convertToNodeSpace(touch.getLocation());
                    var idx = Math.floor(locationInNode.x / self._paiPos) - 1;
                    if (idx >= self._paiCount) {
                        idx = self._paiCount - 1;
                    }
                    if (self.choose_idx == idx)return true;
                    self.choose_idx = idx;
                    self.upPai(self.choose_idx);
                    self.shou.setVisible(false);
                    self.btn_buqie.setVisible(false);
                    return true;
                }
                return false;
            },
            onTouchMoved: function (touch, event) {
                if (TouchUtil.isTouchMe(self.huadong, touch, event)) {
                    var locationInNode = self.huadong.convertToNodeSpace(touch.getLocation());
                    var idx = Math.floor(locationInNode.x / self._paiPos) - 1;
                    if (idx >= self._paiCount) {
                        idx = self._paiCount - 1;
                    }
                    if (self.choose_idx == idx)return;
                    self.choose_idx = idx;
                    self.upPai(self.choose_idx);
                }
            },
            onTouchEnded: function (touch, event) {
                network.sendPhz(5000, "Qiepai/" + self.choose_idx);
            }
        }), self.huadong);
    },
    upPai: function (idx) {
        var self = this;
        if (idx >= 0 && idx < self._paiCount) {
            self.numbg.x = (idx + 1.5) * self._paiPos;
            self.index.setString(idx + 1);
            self.loadingBar.setPercent(100 - self.numbg.x / self.loadingBar.getContentSize().width * 100)
        }
    },
    downPai: function (idx) {
        var self = this;
        if (idx >= 0 && idx < self._paiCount) {
            var pai = self.paisNode.getChildByName('pai' + idx);
            pai.y = 0;
        }
    }
});